import { defineStore } from "pinia";
import { getFound } from "@/api/found";


type state = {
    foundData:listType[],

}
export const usefound = defineStore(
    'found',
    {
        state:():state=>{
            return {
                foundData:[],
            }
        },
        getters:{},
        actions:{
            async fetchGetFoundData(keyword:string){
                let v = keyword.trim();
                // 如果什么也没有输入
                if(!v){
                    this.foundData = [];
                    console.log('v结束');
                    return
                }
                // 输入了内容
                let res =await getFound(v);
                // 找到相关内容
                if(res){
                    this.foundData = res.data;
                    // 先将秒杀的放在前面
                    let isK = this.foundData.filter(item=>item.isseckill==1).sort((a,b)=>a.discount-b.discount);
                    let noK = this.foundData.filter(item=>item.isseckill ==0).sort((a,b)=>a.discount-b.discount);
                    this.foundData = [...isK,...noK];
                    // 再根据折扣进行排序
                // 找不到相关内容
                }else{
                    this.foundData = [];
                }
            },

        }
    }
)